import { keyData } from './mysql-data'
const { $fn, $ } = window
const Index = ({ database, table_name, tableRef}) => {
	const config = {
		modal:{
			title 	: <span>【<b className='c3 ar'>{table_name}</b>】添加索引</span>,
			width 	: '40%',
		},
		form: {
			labelWidth: 's5',
			api: 'mysql/structure/add_key',
			way:'put',
			infoApi : false,
			param: { database, table_name },
			// onValid: ({ Key  }) => {
			// 	if( Key === 'PRI' ){
			// 		let d = $.ref(tableRef).tableRef().data().some( v => v.Key === 'PRI')
			// 		if( d ){
			// 			$fn.msg.warning('一个表中只能有一个主键: Primary Key')
			// 			return false
			// 		}
			// 	}
			// 	return true
			// },
			data: [
				{ label: '类型', name: 'Key', type:'select', data:keyData, r:1, change:({ ref, value }) => {
					$fn.getRow(config, 'index_name').hide = value === 'PRI'
					ref.update()
				}},
				{ label: '索引名称', name: 'index_name', r:1, hide:1},
				{ label: '字段', name: 'Field', type:'select', data:keyData, mul:1, listHeight:600, r:1, change:({ ref, value })=>{
					ref.setValue({ index_name: value?.[0] })
				}},
			],
			onOk : () => {
				$.ref(tableRef).tableRef().search({ scroll:0 })
			}
		},
		onEnd: ({ update, tableRef }) => {
			$fn.getRow(config, 'Field').data = tableRef.data().map( v => v.Field )
			update()
		}
	}
	return config
}

export default Index